<?php
class Thumb{
	public static function grabThumb($url){
		$url = NParseUrl::getUrlinfo($url);
		$result = file_get_contents("http://222.197.171.137:8001/webthumb.php?url=".urlencode($url).'&x=140&y=100');
		//$result = iconv("GBK","UTF-8//IGNORE",$result);
		$grabImage = new GrabImage("http://222.197.171.137:8001/photo/".$result);
		$bigImage = $grabImage->savePic();
		$bigImage = '/' . str_replace('\\','/',$bigImage);
		return $bigImage;
	}
}

class GrabImage{
	private $_url = '';
	private $_filename = '';
	private $_folder = 'urlthumb';
	private $_ext = '';
	function __construct($url){
		$this->_url = $url;
		$this->_ext = strrchr($url,".");
		$this->_savePath = $this->getPath();
		$this->_saveName = md5($this->_url).$this->_folder;
		if(!is_dir($this->_savePath)){
			@mkdir($this->_savePath,0700,1);
		}
	}
	function randomkeys($length){
        	$key='';
        	$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
   			for($i=0;$i<$length;$i++){
   				$key .= $pattern{rand(0,35)};
   			}
   			return $key;
  	   }

	public function getPath(){
		return $this->_folder.DS .date("Y"). DS .date("m"). DS.date("d") . DS;
	}
	public function getSavePath(){
		return $this->_savePath . $this->_saveName . $this->_ext;
	}
	public function savePic(){
		$filename = $this->getSavePath();
		if(is_file($filename)){
			return $filename;
		}else{
			ob_start(); 
			readfile($this->_url); 
			$img = ob_get_contents(); 
			ob_end_clean(); 
			$size = strlen($img); 
			$fp2=@fopen($filename, "a"); 
			fwrite($fp2,$img); 
			fclose($fp2); 
			return $filename; 
		}
	}
}